package com.chixing.controller;

import com.chixing.commons.Result;
import com.chixing.commons.ServerResponse;
import com.chixing.pojo.CompanyAndJobs;
import com.chixing.pojo.CompanyDetail;
import com.chixing.pojo.CompanyQuery;
import com.chixing.pojo.JobAndManage;
import com.chixing.service.ICompanyDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 公司表 前端控制器
 * </p>
 *
 * @author Jason Marslyon
 * @since 2022-10-04
 */
@Controller
public class CompanyDetailController {
    @Autowired
    private ICompanyDetailService companyDetailService;

    @GetMapping("/companyDetail/{id}")
    @ResponseBody
    public ServerResponse getById(@PathVariable("id") Integer companyId){
        return companyDetailService.getById(companyId);
    }

    @PostMapping("/companyDetail")
    @ResponseBody
    public ServerResponse save(CompanyDetail companyDetail){
        return companyDetailService.save(companyDetail);
    }

    @GetMapping("companyDetail")
    /*@ResponseBody*/
    public ModelAndView getAllCompanyDetail(){
        ServerResponse result = companyDetailService.getAll();
        ModelAndView mv = new ModelAndView();
        mv.addObject("result", result);
        mv.setViewName("index");
        return mv;
    }

    /**
     *
     * 查询公司首页  ->  默认页面加载完，公司条件为null
     *
     * @param
     * @return
     */
    @PostMapping("companyDetail/selectAllCompany")
    @ResponseBody
    public Result selectByPageNum(CompanyQuery companyQuery){
        ServerResponse result = companyDetailService.getAllCompany(companyQuery);
        return new Result(20000,result.getReason(),result.getData());
    }

    @GetMapping("companyDetail/toCompanyDetail")
    @ResponseBody
    public ModelAndView toCompanyDetail(Integer companyId){
        CompanyAndJobs companyAndJobs = companyDetailService.getCompanyAndJobs(companyId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("result",companyAndJobs);
        mv.addObject("jobNums",companyAndJobs.getJobDetailList().size());
        //查询该公司有多少为hr
        Integer companyManages = companyDetailService.getCompanyManages(companyId);
        mv.addObject("companyManages",companyManages);
        //一个工作对应一个hr
        List<JobAndManage> jobAndManages = companyDetailService.getJobAndManage(companyId);
        mv.addObject("jobAndManages",jobAndManages);
        mv.setViewName("public/Company/CompanyDetails");
        return mv;

    }

/*   @GetMapping("/companyDetail1")
   @ResponseBody
    public ServerResponse getAll(){
       return companyDetailService.getAll();
   }*/



    /*
     *   后台模块
     */
    @RequestMapping("backStageCompanyDetail/selectById")
    @ResponseBody
    public Result selectById(Integer companyId){
        CompanyDetail companyDetail = companyDetailService.selectById(companyId);
        if(companyDetail!=null) {
            return new Result(20000, "查询成功", companyDetail);
        }
        return new Result(20001,"查询失败",null);
    }

    @RequestMapping("backStageCompanyDetail/insert")
    @ResponseBody
    public Result insert(CompanyDetail companyDetail, HttpSession session){
        boolean b = companyDetailService.insertCompanyDetail(companyDetail);
        CompanyDetail companyDetailInsert = companyDetailService.selectByName(companyDetail.getCompanyName());
        if(b) {
            session.setAttribute("companyDetail", companyDetailInsert);
            return new Result(20000, "添加成功", companyDetailInsert);
        }
        return new Result(20001,"添加失败",null);
    }

    @RequestMapping("backStageCompanyDetail/update")
    @ResponseBody
    public Result update(CompanyDetail companyDetail, HttpSession session){
        boolean b = companyDetailService.updateCompanyDetail(companyDetail);
        CompanyDetail companyDetailUpdate = companyDetailService.selectById(companyDetail.getCompanyId());
        if(b) {
            session.setAttribute("companyDetail", companyDetailUpdate);
            return new Result(20000, "修改成功", companyDetailUpdate);
        }
        return new Result(20001,"修改失败",null);
    }


}
